# Spring Boot 使用 Redis
# 一、引入依赖
在 pom.xml
中引入 Redis
依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 二、redis 配置
在 application.yml
中配置 Redis
参数。
spring:
redis:
host: 127.0.0.1 # 本地 redis 服务器地址
port: 6379
timeout: 1000
lettuce:
pool:
max-active: 20
min-idle: 5
max-wait: 5000ms
max-idle: 20
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 三、通过 RedisTemplate 使用 redis
如果是本地测试,记得把 Redis
服务器启动,否则会无法连接。
@SpringBootTest
class LearnredisApplicationTests {
@Autowired
private RedisTemplate<String, String> strRedisTemplate;
/**
* 测试 Redis 字符串
*/
@Test
public void testRedisString() {
ValueOperations ops = strRedisTemplate.opsForValue();
ops.set("name", "yunhu");
// output: yunhu
System.out.println(strRedisTemplate.opsForValue().get("name"));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 四、自定义 RedisTemplate
默认的 RedisTemplate
只能处理字符串,通过设置序列化器,可以方便的操作实例对象。
# 4.1 新建实体类
一个类只要实现了 Serializable
就可以将其持续的序列化保存。
@Data
public class UserEntity implements Serializable {
private Long id;
private String userName;
private String userSex;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 4.2 设置自定义 RedisTemplate
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
public class RedisConfig {
/**
* 设置序列化
* @param connectionFactory
* @return
*/
@Bean
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
// key 采用 String 的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 4.3 测试自定义 RedisTemplate
@SpringBootTest
class LearnredisApplicationTests {
@Autowired
private RedisTemplate<String, Serializable> serializableRedisTemplate;
/**
* 测试 Redis 序列化对象
*/
@Test
public void testSerializable() {
UserEntity user = new UserEntity();
user.setId(1L);
user.setUserName("yunhu");
user.setUserSex("male");
ValueOperations ops = serializableRedisTemplate.opsForValue();
ops.set("currentUser", user);
UserEntity resultUser = (UserEntity) ops.get("currentUser");
System.out.println("user:" + resultUser.getId()+"," + resultUser.getUserName() + "," + resultUser.getUserSex());
// output: user:1,yunhu,male
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21